﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Components;

namespace WorkflowDesigner
{
    public partial class SolutionNavigator : UserControl
    {
        public event OnLocalWorkflowTreeNodeSelected LocalWorkflowTreeNodeSelected;
        public event OnServerWorkflowTreeNodeSelected ServerWorkflowTreeNodeSelected;

        private List<string> localWorkflowNames;
        private List<string> serverWorkflowNames;

        public SolutionNavigator()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.SolutionTree.Nodes.Clear();
            Liquid.Node localWorkflowRootNode = new Liquid.Node("Local workflows", "Local workflows", false);
            this.SolutionTree.Nodes.Add(localWorkflowRootNode);
            this.localWorkflowNames = WorkflowFALC.ListWorkflows();
            if (this.localWorkflowNames != null && this.localWorkflowNames.Count > 0)
            {
                localWorkflowRootNode.Nodes.Clear();
                localWorkflowRootNode.HasChildren = true;
                foreach (string workflowName in this.localWorkflowNames)
                {
                    Liquid.Node tnWorkflow = new Liquid.Node(workflowName, workflowName, false);
                    localWorkflowRootNode.Nodes.Add(tnWorkflow);
                    tnWorkflow.MouseLeftButtonUp += new MouseButtonEventHandler(tnWorkflow_MouseLeftButtonUp);
                }
                localWorkflowRootNode.ExpandAll();
            }
            

            Liquid.Node serverWorkflowRootNode = new Liquid.Node("Published workflows", "Published workflows", false);
            this.SolutionTree.Nodes.Add(serverWorkflowRootNode);
            serverWorkflowRootNode.HasChildren = false;

            WorkflowDesign wfDesign=new WorkflowDesign();
            wfDesign.PublishedWorkflowsUpdated += new WorkflowDesign.OnPublishedWorkflowsUpdated(wfDesign_PublishedWorkflowsUpdated);
            wfDesign.GetPublishedWorkflowDesigns();
        }

        void wfDesign_PublishedWorkflowsUpdated(List<string> workflowNames)
        {
            this.serverWorkflowNames = workflowNames;
            if (this.serverWorkflowNames != null && this.serverWorkflowNames.Count > 0)
            {
                Liquid.Node root = null;
                foreach(Liquid.Node rootNode in this.SolutionTree.Nodes)
                {
                    if (rootNode.Title == "Published workflows")
                    {
                        root = rootNode;
                        break;
                    }
                }
                if(root !=null)
                {
                    root.Nodes.Clear();
                    root.HasChildren = true;
                    foreach (string workflowName in this.serverWorkflowNames)
                    {
                        Liquid.Node tnWorkflow = new Liquid.Node(workflowName, workflowName, false);
                        root.Nodes.Add(tnWorkflow);
                        tnWorkflow.MouseLeftButtonUp += new MouseButtonEventHandler(tnWorkflow_MouseLeftButtonUp2);
                    }
                    root.ExpandAll();
                }
            }
            this.SolutionTree.ExpandAll();
        }

        void tnWorkflow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (sender is Liquid.Node)
            {
                Liquid.Node tn = sender as Liquid.Node;
                if (this.LocalWorkflowTreeNodeSelected != null)
                    this.LocalWorkflowTreeNodeSelected(tn.ID);
            }
        }

        void tnWorkflow_MouseLeftButtonUp2(object sender, MouseButtonEventArgs e)
        {
            if (sender is Liquid.Node)
            {
                Liquid.Node tn = sender as Liquid.Node;
                if (this.ServerWorkflowTreeNodeSelected != null)
                    this.ServerWorkflowTreeNodeSelected(tn.ID);
            }
        }
    }
}
